package com.apache.gateway.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.apache.tools.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @description: 缓存操作类
 * @author: Hou Dayu
 * @date: Created in 2021/2/2
 */
@Component
public class JedisUtil {

    private Logger logger = LoggerFactory.getLogger("gateway-logs");

    @Autowired
    private JedisPool jedisPool;

    public void setRoutsCache(String spaceName,String cacheKey, Map<String, String> route){
        Jedis jedis = null;
        try{
            spaceName = StrUtil.doNull(spaceName,"zuul.routs");
            jedis = jedisPool.getResource();
            jedis.select(10);
            jedis.hdel(spaceName,cacheKey);
            jedis.hsetnx(spaceName,cacheKey, JSON.toJSONString(route));
        }catch (Exception e){
        }finally{
            close(jedis);
        }
    }

    public JSONObject getRoute(String spaceName, String serviceId){
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(10);
            String result = jedis.hget(StrUtil.doNull(spaceName,"zuul.routs"),serviceId);
            if(StrUtil.isNotNull(result)){
                return JSON.parseObject(result);
            }
        }catch (Exception e){
        }finally{
            close(jedis);
        }
        return new JSONObject();
    }

    public void buildCacheInfo(String key, String value, int expireTime){
        Jedis jedis = null;
        boolean mark = true;
        try{
            jedis = jedisPool.getResource();
            jedis.select(1);
            jedis.del(key);
            jedis.setnx(key,value);
            if(expireTime>0)
                jedis.expire(key,expireTime);
        }catch (Exception e){
        }finally{
            close(jedis);
        }
    }

    public String getCahceInfo(String key){
        if(StrUtil.isNull(key)){
            return null;
        }
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(1);
            return jedis.get(key);
        }catch (Exception e){
            return null;
        }finally{
            close(jedis);
        }
    }

    public boolean createObjCache(String key, String value, int expireTime,int database) {
        Jedis jedis = null;
        boolean mark = true;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            jedis.del(key);
            jedis.setnx(key,value);
            if(expireTime>0)
                jedis.expire(key,expireTime);
        }catch (Exception e){
            mark =false;
            logger.error("执行方法createObjCache出错：",e);
        }finally{
            close(jedis);
        }
        return mark;
    }

    public boolean delObjCache(String key,int database) {
        Jedis jedis = null;
        boolean mark = true;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            jedis.del(key);
        }catch (Exception e){
            mark =false;
            logger.error("执行方法delObjCache出错：",e);
        }finally{
            close(jedis);
        }
        return mark;
    }

    public Object getlObjCache(String key,int database) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            return jedis.get(key);
        }catch (Exception e){
            logger.error("执行方法getlObjCache出错：",e);
            return null;
        }finally{
            close(jedis);
        }
    }

    public String getCacheInfo(String key, int database) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            return jedis.get(key);
        }catch (Exception e){
            logger.error("执行方法getlObjCache出错：",e);
            return null;
        }finally{
            close(jedis);
        }
    }

    public boolean createObjCache(String spaceName, String key, String value, int expireTime,int database) {
        Jedis jedis = null;
        boolean mark = true;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            jedis.hdel(spaceName,key);
            jedis.hset(spaceName,key,value);
            if(expireTime>0)
                jedis.expire(key,expireTime);
        }catch (Exception e){
            logger.error("执行方法createObjCache出错：",e);
            mark =false;
        }finally{
            close(jedis);
        }
        return mark;
    }

    public boolean delObjCache(String spaceName, String key,int database) {
        Jedis jedis = null;
        boolean mark = true;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            jedis.hdel(spaceName,key);
        }catch (Exception e){
            logger.error("执行方法delObjCache出错：",e);
            mark =false;
        }finally{
            close(jedis);
        }
        return mark;
    }

    public Object getObjCache(String spaceName, String key,int database) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            return jedis.hget(spaceName,key);
        }catch (Exception e){
            logger.error("执行方法getObjCache出错：",e);
        }finally{
            close(jedis);
        }
        return null;
    }

    public String getCacheInfo(String spaceName, String key, int database) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            return jedis.hget(spaceName,key);
        }catch (Exception e){
            logger.error("执行方法getObjCache出错：",e);
        }finally{
            close(jedis);
        }
        return null;
    }

    public Map getObjCache(String spaceName,int database) {
        Jedis jedis = null;
        try{
            jedis = jedisPool.getResource();
            jedis.select(database);
            return jedis.hgetAll(spaceName);
        }catch (Exception e){
            logger.error("执行方法getObjCache出错：",e);
        }finally{
            close(jedis);
        }
        return null;
    }

    protected void close(Jedis jedis){
        if(null != jedis)
            jedis.close();
    }
}
